package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/submit")
    public Result submitOrder(@RequestBody Map<String,Object> map) throws Exception {
        Result result = null;
        String key = (String) map.get("telephone") + "_" +  RedisMessageConstant.SENDTYPE_ORDER;

        String validateCode = (String) map.get("validateCode");
        String redisInCode = jedisPool.getResource().get(key);

        if (null==redisInCode){
            return new Result(false,"请重新获取验证码");
        }

        if (!validateCode.equals(redisInCode)){
            return new Result(false,"您输入的验证码不正确");
        }
        map.put("orderType","微信预约");
        result = orderService.submitOrder(map);
        return result;
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Map map = orderService.findById(id);
        return new Result(false,"查询成功",map);
    }
}